package cn.mesmile.juc.readwrite;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author zb
 * @date 2021/8/17 15:46
 * @Description 写锁 降级 成为 读锁
 */
public class DemoteLock {

    /** 写锁降级的过程 */
    public static void main(String[] args) {
        ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
        // 写锁
        ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();
        // 读锁
        ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();
        // ① 获取写锁
        writeLock.lock();
        System.out.println("11111111111111111111");
        // ② 获取读锁
        readLock.lock();
        System.out.println("22222222222222222222");
        // ③ 释放写锁
        writeLock.unlock();
        System.out.println("33333333333333333333");
        // ④ 释放读锁
        readLock.unlock();
        System.out.println("44444444444444444444");
    }

}
